package schach.server.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import schach.GameController;
import schach.server.model.Feld;
import schach.server.view.Schachbrett;
import schach.GameController;

public class ServerController {

	private ServerSocket server = new ServerSocket(1234);
	private GameController controller;
	private Socket client;
	private Schachbrett brett;
	boolean empfangeRuns = false;
	private InputStream input;
	public ServerController(Schachbrett brett) throws IOException{
		this.brett = brett;
		Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
		controller = (GameController) UnicastRemoteObject.exportObject(new GameControllerImpl(),
				0);
		reg.rebind("GameController", controller);
		Thread verbindungThread = new Thread(new Verbindung());
		verbindungThread.start();
		System.out.println("Danach");

	}
	public GameController getController(){
		return controller;
	}
	public void sendeErgebni(boolean spiel1){
		String page = "http://teamzoo.ath.cx:8080/gamelog/";
		WebDriver driver = new FirefoxDriver();
		int zuegeG = 0;
		driver.get(page + "insert.php");
		WebElement spieler1 = driver.findElement(By.id("spieler1"));
		WebElement spieler2 = driver.findElement(By.id("spieler2"));
		WebElement zuege = driver.findElement(By.id("anzahl_zuege"));
		if (spiel1){
			spieler1.sendKeys("1");
			spieler2.sendKeys("0");
		}
		else{
			spieler1.sendKeys("0");
			spieler2.sendKeys("1");
		}
		try {
			for(ArrayList<Feld> reihen : this.getController().getBrett().getFelder()){
				for(Feld feld : reihen){
					try{
						zuegeG += feld.getFigur().getNoOfMoves();
					} catch (NullPointerException e){}
				}
				
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		zuege.sendKeys("" + zuegeG);
		zuege.submit();
		driver.get(page + "log.php");
	}

	public boolean getEmpfangeRuns(){
		return empfangeRuns;
	}
	public void empfange(){
		Thread empfange = new Thread(new EmpfangeZug());
		if (client != null){
			empfange.start();
			empfangeRuns = true;
		}
	}
	private class EmpfangeZug implements Runnable{
	
		@Override
		public void run() {
			System.out.println("Empfange runs");
			
				try {
					
					System.out.println("Empfange davor");
					input = client.getInputStream();
					System.out.println("Nach Exception");
					if (input != null){
						if(input.read() == 10){
							brett.setAmZug(true);
							brett.paint(brett.getGraphics());
							//input.close();
							System.out.println("Empfangen server");
						}
					} else{
						System.out.println("Input null");
					}
					
				}
			catch (SocketException e){
				System.out.println("Socket Exception");
			}
			catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
		}
	}
	

	public void sendeZug(){
		Thread senden = new Thread(new ZugSendung());
		senden.start();
	}
	private class ZugSendung implements Runnable{
		
		public void run(){
			try {
				OutputStream output = client.getOutputStream();
				output.write(10);
				output.flush();
				brett.setAmZug(false);
				System.out.println("Gesendet Server");
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private class Verbindung implements Runnable{

	
	public Verbindung(){
		
	}
	public void run(){
		System.out.println("Hello");
		try{
			while (true){
				client = server.accept();
				System.out.println("Thread");
				}
			}
			catch (IOException e){
			}
		}
	}
}


